package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.*;
import cn.gson.boot.model.mapper.*;
import cn.gson.boot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class WxgdTuiHuanService {
    @Autowired
    WxgdTuiHuanMapper wxgdTuiHuanMapper;
    @Autowired
    WxgdTuiHuanDao wxgdTuiHuanDao;
    @Autowired
    WxgdTuiHuanXiangQinDao wxgdTuiHuanXiangQinDao;
    @Autowired
    HuanHuoDao huanHuoDao;
    @Autowired
    TuiHuoDao tuiHuoDao;

    //查看所有维修工单
    public List<Wxgdtuihuan> selectAllwxTuiHuan(String gdHao,String time,String empName) {
        System.out.println("ServiceName:"+empName);
        return wxgdTuiHuanMapper.selectAllwxTuiHuan(gdHao,time,empName);
    }

    public void addwxgdTuiHuan(Wxgdtuihuan wxgdtuihuan) {
        Wxgdtuihuan save = wxgdTuiHuanDao.save(wxgdtuihuan);
        Wxgdtuihuanxiangqin wxth = null;
        for(int i=0;i<save.getWxgdtuihuanxiangqinList().size();i++){
            wxth = new Wxgdtuihuanxiangqin();
            wxth.setWxgdtuihuan(save);
            wxth.setProductt(save.getWxgdtuihuanxiangqinList().get(i).getProductt());
            wxth.setGd2Num(save.getWxgdtuihuanxiangqinList().get(i).getGd2Num());
            wxgdTuiHuanXiangQinDao.save(wxth);
        }
        //修改退换货的维修完成状态
        if(wxgdtuihuan.getGd2Thhh().equals("退货")){
            tuiHuoDao.updateTuiHuoWxwc(0,wxgdtuihuan.getGd2Thhhid());
        }else if(wxgdtuihuan.getGd2Thhh().equals("换货")){
            huanHuoDao.updateHuanWxwc(0,wxgdtuihuan.getGd2Thhhid());
        }

    }

    //修改维修退换货的维修完成状态和维修的完成时间
    public void updatewxtuihuanWcZtai(Integer gd2Wx,Integer gd2Id,String sm,Integer smId){
        wxgdTuiHuanDao.updatewxtuihuanWcZtai(gd2Wx,gd2Id);
        wxgdTuiHuanDao.updatewxtuihuanWcTime(new Timestamp(new Date().getTime()),gd2Id);
        //修改退换货的维修完成状态
        if(sm.equals("退货")){
            tuiHuoDao.updateTuiHuoWxwc(gd2Wx,smId);
        }else if(sm.equals("换货")){
            huanHuoDao.updateHuanWxwc(gd2Wx,smId);
        }
    }

    //修改维修退换货的入库状态
    public void updatewxtuihuanRuKuZtai(Integer gd2Rk,Integer gd2Id){
        wxgdTuiHuanDao.updatewxtuihuanRuKuZtai(gd2Rk,gd2Id);
    }
}